package com.ruoyi.web.core.config;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class  LoggingRequestInterceptor implements ClientHttpRequestInterceptor {

    //final static Logger log = LoggerFactory.getLogger(LoggingRequestInterceptor.class);

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
//        LogProxy log = SpringContextUtil.getApplicationContext().getBean(LogProxy.class);
        System.out.println("===========================gateway api begin================================================");
        traceRequest(request, body);
        long begin = System.currentTimeMillis();
        ClientHttpResponse response = execution.execute(request, body);
        long consume = System.currentTimeMillis() - begin;
        traceResponse(response);
        System.out.println("请求耗时:{} ms"+ consume);
        System.out.println("===========================gateway api end================================================");
        return response;
    }

    private void traceRequest(HttpRequest request, byte[] body) throws IOException {
//        LogProxy log = SpringContextUtil.getApplicationContext().getBean(LogProxy.class);
        System.out.println("===========================request begin================================================");
        System.out.println("URI         : {}" + request.getURI());
        System.out.println("Method      : {}" + request.getMethod());
        System.out.println("Headers     : {}" + request.getHeaders() );
        System.out.println("Request body: {}"+ new String(body, "UTF-8"));
        System.out.println("==========================request end================================================");
    }

    private void traceResponse(ClientHttpResponse response) throws IOException {
//        LogProxy log = SpringContextUtil.getApplicationContext().getBean(LogProxy.class);
        StringBuilder inputStringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), "UTF-8"));
        String line = bufferedReader.readLine();
        while (line != null) {
            inputStringBuilder.append(line);
            inputStringBuilder.append('\n');
            line = bufferedReader.readLine();
        }
        System.out.println("============================response begin==========================================");
        System.out.println("Status code  : {}"+ response.getStatusCode());
        System.out.println("Status text  : {}"+ response.getStatusText());
        System.out.println("Headers      : {}"+ response.getHeaders());
        System.out.println("Response body: {}"+ inputStringBuilder.toString());
        System.out.println("=======================response end=================================================");
    }

}
